// pages/mine/mypassword/mypassword.js
var app=getApp();
Page({

  /**
   * 页面的初始数据
   */
  data: {
    url:'',
    oldPassword:'',
    newPassword:'',
    password:'',
    userInfor:''
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad(options) {
    this.setData({
        url:app.globalData.URL,
        userInfor:app.globalData.userInfor
     })
  },

  oldChange: function (e) {
    this.setData({
        'oldPassword' : e.detail.value
    });
  }, 
  newChange: function (e) {
    this.setData({
        'newPassword' : e.detail.value
    });
  }, 
  passChange: function (e) {
    this.setData({
        'password' : e.detail.value
    });
  }, 


  editPassword(){
      let that = this
    wx.request({
      url: that.data.url + 'us/passwordCheck/?p1='+that.data.userInfor.password+"&un="+that.data.userInfor.username+"&p2="+that.data.oldPassword,
      success:function (res) {
          //console.log(res);
          if(res.data.code==1){
              //console.log(111);
              if((that.data.newPassword!=null&&that.data.newPassword!='')&&that.data.newPassword==that.data.password&&that.data.password.length>4){
                wx.request({
                  url: that.data.url + 'user/editPassword',
                  method:'PUT',
                  data:{
                      id:that.data.userInfor.id,
                      username:that.data.userInfor.username,
                      password:that.data.password
                  },
                  success:function (res) {
                      //console.log(res);
                    if(res.data.code==1){
                        app.globalData.loginNum=0,
                        app.globalData.userInfor='',
                        wx.redirectTo({
                          url: '/pages/mine/login/login',
                        })
                        //console.log("成功！");
                    }else{
                        //console.log(222);
                    }
                  }
                })
              }else{
                wx.showModal({
                    title: '新密码错误',
                    content: '新密码格式错误或两次密码不相等，请重试！',
                    success: function (res) {
                    if (res.confirm) {
                        that.setData({
                            'oldPassword' : '',
                            'newPassword' : '',
                            'password': ''
                          });
                    } else {
                        wx.switchTab({
                            url: '/pages/mine/mine',
                            })
                        }
                    }
                });
              }

          }else{
            wx.showModal({
                title: '密码错误',
                content: '密码错误，请重试！',
                success: function (res) {
                if (res.confirm) {
                    that.setData({
                        'oldPassword' : '',
                        'newPassword' : '',
                        'password': ''
                      });
                } else {
                    wx.switchTab({
                        url: '/pages/mine/mine',
                        })
                    }
                }
            });
          }
      }
    })
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady() {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow() {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide() {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload() {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh() {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom() {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage() {

  }
})